<?php
/**
+-------------------------------------------------------------------------------------------------------
* GouGuOPEN [ 左手研发，右手开源，未来可期！]
+-------------------------------------------------------------------------------------------------------
* @Copyright (c) http://blog.gougucms.com All rights reserved.
+-------------------------------------------------------------------------------------------------------
* @Licensed 勾股系列相关开源系统，可免费使用，但并不是自由软件，未经授权许可，不能去除系统的相关版权信息
+-------------------------------------------------------------------------------------------------------
* @Author 勾股工作室 <hdm58@qq.com>
+-------------------------------------------------------------------------------------------------------
*/
declare (strict_types = 1);

namespace app\honor\controller;

use app\base\BaseController;
use app\train\model\Train;
use app\train\model\Paper;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Statistics extends BaseController
{		
	public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
			$where = [];
			if (!empty($param['keywords'])) {
                $where[] = ['a.name', 'like', '%' . $param['keywords'] . '%'];
            }
			if (!empty($param['dids'])) {
                $where[] = ['a.did', 'in', $param['dids']];
            }
			if (!empty($param['pids'])) {
                $where[] = ['a.position_id', 'in', $param['pids']];
            }
			$where[] = ['a.status', '=', 1];
            $list = Db::name('Admin')
				->field('a.id,a.name,a.thumb,a.did,a.position_id,a.type,a.integral,a.credit,d.title as department,p.title as position')
				->alias('a')
				->join('department d','d.id = a.did')
				->join('position p','p.id = a.position_id')
				->where($where)
				->order('a.id desc')
				->select()
				->toArray();
			$model = new Train();
			foreach ($list as $key => &$value) {
				$total = $model->total($value['id'],$value['did'],$value['position_id']);
				
				$value['course_learning'] = $total['course_learning'];
				$value['course_finish'] = $total['course_finish'];
				
				$value['train_unfinish'] = $total['train_unfinish'];
				$value['train_finish'] = $total['train_finish'];
				
				$value['learning_unfinish'] = $total['learning_unfinish'];
				$value['learning_finish'] = $total['learning_finish'];
				
				$value['position_unfinish'] = $total['position_unfinish'];
				$value['position_finish'] = $total['position_finish'];
				
				$value['paper_unfinish'] = $total['paper_unfinish'];
				$value['paper_finish'] = $total['paper_finish'];
				
				$value['certificate'] = Db::name('CertificateUsers')->where('user_id', $value['id'])->count();
			}		
			$res['data'] = $list;
            return table_assign(0, '', $res);
        } else {
            return view();
        }
    }
    //学员排行榜
    public function ranking()
    {
		$rank_credit = Db::name('Admin')
				->field('a.id,a.name,a.thumb,a.did,a.position_id,a.type,a.integral,a.credit,d.title as department,p.title as position')
				->alias('a')
				->join('department d','d.id = a.did')
				->join('position p','p.id = a.position_id')
				->where(['a.status'=>1])
				->order('a.credit desc')
				->select()
				->toArray();
				
		$rank_integral = Db::name('Admin')
				->field('a.id,a.name,a.thumb,a.did,a.position_id,a.type,a.integral,a.credit,d.title as department,p.title as position')
				->alias('a')
				->join('department d','d.id = a.did')
				->join('position p','p.id = a.position_id')
				->where(['a.status'=>1])
				->order('a.integral desc')
				->select()
				->toArray();

		View::assign('rank_credit', $rank_credit);
		View::assign('rank_integral', $rank_integral);
		return view();
    }
	
	//学员学习情况
    public function overview()
    {
        $param = get_params();
		$admin = Db::name('Admin')->where('id',$param['id'])->find();
		$admin['department'] = Db::name('Department')->where('id',$admin['did'])->value('title');
		$model = new Train();
		View::assign([
			'course' => $model->user_course($param['id']),
			'train' => $model->user_train($param['id']),
			'learning' => $model->user_learning($param['id']),
			'position' => $model->user_position($param['id']),
			'paper' => $model->user_paper($param['id']),
			'certificate' => $model->user_certificate($param['id']),
			'admin' => $admin			
		]);
		return view();
    } 
	
	//我的学习情况
	public function mylearn()
    {
        $param = get_params();
		$admin = Db::name('Admin')->where('id',$param['id'])->find();
		$admin['department'] = Db::name('Department')->where('id',$admin['did'])->value('title');
		$model = new Train();
		View::assign([
			'course' => $model->user_course($param['id']),
			'train' => $model->user_train($param['id']),
			'learning' => $model->user_learning($param['id']),
			'position' => $model->user_position($param['id']),
			'paper' => $model->user_paper($param['id']),
			'certificate' => $model->user_certificate($param['id']),
			'admin' => $admin			
		]);
		return view();
    }
   
}
